//
//  UITableView+EmptyTitle.m
//  customer
//
//  Created by Holothurian on 2018/11/8.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "UITableViewEmptyTitle.h"

@implementation UITableView (EmptyTitle)

static char emptyTitleKey;
static char emptyBackViewKey;
static char emptyImgKey;

/// 空记录提示语
- (void)setEmptyTitle:(NSString *)emptyTitle
{
    objc_setAssociatedObject(self, &emptyTitleKey, emptyTitle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString*) emptyTitle
{
    return objc_getAssociatedObject(self, &emptyTitleKey);
}

- (void)setEmptyBackView:(UIView *)emptyBackView
{
    objc_setAssociatedObject(self, &emptyBackViewKey, emptyBackView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
/// 空记录 view
- (UIView*) emptyBackView
{
    return objc_getAssociatedObject(self, &emptyBackViewKey);
}

- (void)setEmptyImg:(NSString *)emptyImg
{
    objc_setAssociatedObject(self, &emptyImgKey, emptyImg, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
/// 空记录 img
- (NSString*) emptyImg
{
    return objc_getAssociatedObject(self, &emptyImgKey);
}


- (void)setShowEmpty:(BOOL)showEmpty
{
    if (showEmpty) {
        [self showEmptyView];
    }else{
        [self hideEmptyView];
    }
}


/// 显示无数据
- (void)showEmptyView;
{
    // 未设置提示语,不显示
    if (self.emptyTitle.length < 1) {return;}
    
    // 为空,创建
    if (!self.emptyBackView) {
        
        float tH = 20;
        float tW = self.viewWidth;
        
        self.emptyBackView = [UIView newView:CGRectMake(0, 0, tW, tH)];
        [self addSubview:self.emptyBackView];
        self.emptyBackView.center = CGPointMake(self.viewWidth/2, self.viewHeight/2.5);
        if (self.tableHeaderView) {
            self.emptyBackView.center = CGPointMake(self.viewWidth/2, self.tableHeaderView.viewHeight+(self.viewHeight-self.tableHeaderView.viewHeight)/3);
        }
        
        UILabel* tLabel = [[UILabel alloc]initWithFrame:self.emptyBackView.bounds];
        tLabel.backgroundColor = [UIColor clearColor];
        tLabel.font = Font28;
        [self.emptyBackView addSubview:tLabel];
        tLabel.text = self.emptyTitle;
        tLabel.textColor = [HSPublic colorWithHexString:@"666666"];
        [tLabel sizeToFit];
        tLabel.center = CGPointMake(self.emptyBackView.viewWidth/2, self.emptyBackView.viewHeight/2);
        
        if (self.emptyImg) {
            UIImageView* tImgView = nil;
            tImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.emptyImg]];
            tImgView.center = CGPointMake(F_DEVICE_W/2, 0-tImgView.viewHeight*0.6);
            [self.emptyBackView addSubview:tImgView];
            
            self.emptyBackView.viewTop += tImgView.viewHeight/4;
        }
    }
    
    self.emptyBackView.hidden = NO;
}

/// 隐藏无数据
- (void)hideEmptyView;
{
    self.emptyBackView.hidden = YES;
}

@end
